namespace IMPlant.Core.Web.Control.GridViewFunction
{
    using IMPlant.Core.Web.Control;
    using IMPlant.Core.Web.Control.Helper;
    using System;
    using System.Drawing;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class MouseOverFunction : ExtendFunction
    {
        public MouseOverFunction()
        {
        }

        public MouseOverFunction(IMPlant.Core.Web.Control.GridView grid) : base(grid)
        {
        }

        private void _grid_RowDataBoundCell(object sender, GridViewTableCell gvtc)
        {
            TableCell tableCell = gvtc.TableCell;
            GridViewRow parent = (GridViewRow) tableCell.Parent;
            if (parent.RowType == DataControlRowType.DataRow)
            {
                this.SetAttribute(tableCell, ChangeType.CellChange);
            }
        }

        private void _grid_RowDataBoundDataRow(object sender, GridViewRowEventArgs e)
        {
            this.SetAttribute(e.Row, ChangeType.RowChange);
        }

        protected override void Execute()
        {
            if ((base._grid.MouseOver != null) && base._grid.MouseOver.ChangeColor)
            {
                base._grid.RowDataBoundDataRow += new IMPlant.Core.Web.Control.GridView.RowDataBoundDataRowHandler(this._grid_RowDataBoundDataRow);
            }
            if ((base._grid.MouseOver != null) && base._grid.MouseOver.CellChangeColor)
            {
                base._grid.RowDataBoundCell += new IMPlant.Core.Web.Control.GridView.RowDataBoundCellHandler(this._grid_RowDataBoundCell);
            }
        }

        private void SetAttribute(IAttributeAccessor iaa, ChangeType type)
        {
            string name = string.Empty;
            if (type == ChangeType.RowChange)
            {
                if (base._grid.MouseOver.OverColor.Name == "0")
                {
                    name = Color.Teal.Name;
                }
                else
                {
                    name = base._grid.MouseOver.OverColor.Name;
                }
            }
            else if (base._grid.MouseOver.CellOverColor.Name == "0")
            {
                name = Color.Pink.Name;
            }
            else
            {
                name = base._grid.MouseOver.CellOverColor.Name;
            }
            name = string.Equals(name.Substring(0, 2), "ff") ? ("#" + name.Substring(2, 6)) : name;
            Common.SetAttribute(iaa, "onmouseover", string.Format("this.oldcolor=this.style.backgroundColor;this.style.backgroundColor='{0}'", name), AttributeValuePosition.Last);
            Common.SetAttribute(iaa, "onmouseout", "this.style.backgroundColor=this.oldcolor", AttributeValuePosition.Last);
        }
    }
}

